//
//  One2DetailViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/2.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "One2DetailViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "Three2Cell.h"
#import "MBProgressHUD+MJ.h"
#define cellHeight 44


@interface One2DetailViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//获取网络信息的数组
@property(nonatomic,strong) NSMutableDictionary *list;
//赋值取得水果的list
@property(nonatomic,strong) NSMutableArray *list2;

//储存每个水果的数组信息用array
@property(nonatomic,strong) NSMutableDictionary *list3;
//赋值取得水果的list
@property(nonatomic,strong) NSMutableDictionary *list4;


//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


@property(nonatomic,strong) UILabel *lblBotttom;
//总价
@property(nonatomic,strong) UILabel *zongjialbl;
//设置所有的底端的数据
@property(nonatomic,strong) NSString *number;
@property(nonatomic,strong) NSString *yunFei;
@property(nonatomic,strong) NSString *huokuan;
@property(nonatomic,strong) NSString *zongjia;


@end




@implementation One2DetailViewController




//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"汇总详情";
    self.backBtn.hidden = NO;
    self.list = [NSMutableDictionary dictionaryWithCapacity:0];
    self.list2 = [NSMutableArray arrayWithCapacity:0];
    self.list3 = [NSMutableDictionary dictionaryWithCapacity:0];
    self.list4 = [NSMutableDictionary dictionaryWithCapacity:0];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BACKGROUND1;
    self.view.backgroundColor = BACKGROUND1;
    

    
    
    
    //设置底端的View
    
    [self initBottom];
    
    
    [self sendAPI];


    
    
    
    
}





-(void)initBottom
{
    
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, ViewHeight-63 , ViewWidth, 1)];
    view2.backgroundColor = BenSe_COLOR;
    [self.view addSubview:view2];
    
    self.lblBotttom = [[UILabel alloc]initWithFrame:CGRectMake(0, ViewHeight-64, ViewWidth, 60)];
    //    self.lblBotttom.backgroundColor = [UIColor greenColor];
    self.lblBotttom.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.lblBotttom];
    
    
    
    
    
    self.zongjialbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth -90, ViewHeight -64, 80, 60)];
    self.zongjialbl.font = [UIFont systemFontOfSize:13];
    self.zongjialbl.textColor = [UIColor grayColor];
    [self.view addSubview:self.zongjialbl];
    
    
    
    
    
    
    
    
}



-(void)sendAPI
{
    NSString *urlStr = [NSString stringWithFormat:@"%@order_summary_info",SERVER_ADDRESS2];
    
    
    
    NSDictionary *params =  @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"order_id" : self.oID };
    
    
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0)
        {
            if (![responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                
                [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
                
                return ;
            }
            else
            {
              
                NSDictionary *dict = responseObject[@"data"][@"info"];
                
            self.list = [NSMutableDictionary dictionaryWithDictionary:dict];
//                NSLog(@"asdhashdjkahsdhqo  %@",self.list);
                
                self.list2 = [NSMutableArray arrayWithArray:[self.list objectForKey:@"order_list"]];
                
//                for (int i = 0; i < self.list2.count; i++) {
//                    
//                    self.list3 = [NSMutableDictionary dictionaryWithDictionary:self.list2[i]];
//                    
//                    self.list4 = [NSMutableArray arrayWithArray:[self.list3 objectForKey:@"product_list"]];
//                    
//                    
//                    
//                }
                
                
//                NSLog(@"222uuuuuuuuu %@",self.list4);
                
                
                
                
                //赋值底端数据
                self.number = [NSString stringWithFormat:@"%d",[[self.list objectForKey:@"near_num"] intValue] + [[self.list objectForKey:@"far_num"] intValue]];
                
                
                
                self.yunFei = [NSString stringWithFormat:@"%.1f",[[self.list objectForKey:@"amount_total"] floatValue] - [[self.list objectForKey:@"product_amount"] floatValue]];
                
                
                self.huokuan = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"product_amount"]];
                
                self.zongjia = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"amount_total"]];
                
                
                
                
                
                
                self.lblBotttom.text =  [NSString stringWithFormat:@"  总送货%@家  运费 ￥%@  货款 ￥%@ ",self.number,self.yunFei,self.huokuan];
                

                
                
                NSString *ZJ  = [NSString stringWithFormat:@"  总金额￥%@",self.zongjia];
                
                self.zongjialbl.font = [UIFont systemFontOfSize:14];
                self.zongjialbl.text = ZJ;
                self.zongjialbl.numberOfLines = 0;
                self.zongjialbl.lineBreakMode = NSLineBreakByWordWrapping;
                CGSize size = [self.zongjialbl sizeThatFits:CGSizeMake(self.zongjialbl.frame.size.width-10, 60)];
                self.zongjialbl.frame = CGRectMake(ViewWidth -90, ViewHeight -64+10, 90, size.height);
                self.zongjialbl.textAlignment = NSTextAlignmentRight;
                self.zongjialbl.textColor = BenSe_COLOR;

                
                
                
                
                
                
                
                
                
                
                
                
                
            }
            
            
            
            [self.tableView reloadData];
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            
            
            
            
        }
        else
        {
        
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        }
        
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];
    
    
    
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list2.count;
}






-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 20;
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    int a = 0;
    NSDictionary *dict = [self.list2 objectAtIndex:indexPath.row];
    
    
    for (int i = 0 ; i < [[dict objectForKey:@"product_list"] count]; i++)
    {
        
        if ([[[dict objectForKey:@"product_list"][i] objectForKey:@"product_name"] length] != 0)
        {
            a++;
        }
        
        
        
    }
    
    return a *40 + 38 + 44 *6 +25 +54 +20;
    
    
    
    
    
    
    
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *CellIdentifier = @"Cell";
    Three2Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell =  [[NSBundle mainBundle] loadNibNamed:@"Three2Cell" owner:self options:nil][0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        
    }
    
    //循环加载显示数据
    
    //店铺照片以及背景
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(8, 0, ViewWidth-16,38)];
    view2.backgroundColor = TiaoColor;
    [cell addSubview:view2];
    
    
    
    
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(3,3,32,32)];
    NSString *shopUrl = [NSString stringWithFormat:@"%@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"shopUrl"]];
    NSString *shopImage = [NSString stringWithFormat:@"%@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"shopImage"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@/%@",shopUrl,shopImage];
    NSURL *url = [NSURL URLWithString:imageUrl];
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"backImage"]];
    [view2  addSubview:imageView];
    
    
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, ViewWidth, 38)];
    lbl1.text = [NSString stringWithFormat:@" %@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"shopName"]];
    lbl1.textColor = [UIColor whiteColor];
    [view2 addSubview:lbl1];
    
    
    UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 , 90, 44)];
    lbl11.textColor = [UIColor grayColor];
    [cell addSubview:lbl11];
    lbl11.text = @"订单编号";
    lbl11.font = [UIFont systemFontOfSize:16];
    
    
    
    UILabel *lbl12 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight, 90, cellHeight)];
    lbl12.textColor = [UIColor grayColor];
    [cell addSubview:lbl12];
    lbl12.text = @"联系电话";
    lbl12.font = [UIFont systemFontOfSize:16];
    
    
    UILabel *lbl13 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight *2, 90, cellHeight)];
    lbl13.textColor = [UIColor grayColor];
    [cell addSubview:lbl13];
    lbl13.text = @"送货距离";
    lbl13.font = [UIFont systemFontOfSize:16];
    
    
    
    
    UILabel *lbl14 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight *2, 90, cellHeight)];
    lbl14.textColor = [UIColor grayColor];
    [cell addSubview:lbl13];
    lbl14.text = @"送货距离";
    lbl14.font = [UIFont systemFontOfSize:16];
    
    
    
    
    UILabel *lbl15 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight *3, 90, cellHeight)];
    lbl15.textColor = [UIColor grayColor];
    [cell addSubview:lbl15];
    lbl15.text = @"下单时间";
    lbl15.font = [UIFont systemFontOfSize:16];
    
    
    UILabel *lbl16 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight *4, 90, cellHeight)];
    lbl16.textColor = [UIColor grayColor];
    [cell addSubview:lbl16];
    lbl16.text = @"送货时间";
    lbl16.font = [UIFont systemFontOfSize:16];
    
    
    
    UILabel *lbl17 = [[UILabel alloc]initWithFrame:CGRectMake(8, 38 +cellHeight *5, 90, cellHeight)];
    lbl17.textColor = [UIColor grayColor];
    [cell addSubview:lbl17];
    lbl17.text = @"送货地址";
    lbl17.font = [UIFont systemFontOfSize:16];
    
    
    
    UILabel *lbl21 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38, 230, cellHeight)];
    lbl21.text = [NSString stringWithFormat:@"%@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"order_id"]];
    lbl21.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl21];
    
    
    
    
    
    UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38 + cellHeight, 230, cellHeight)];
    lbl22.text = [NSString stringWithFormat:@"%@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"sTel"]];
    lbl22.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl22];
    
    
    
    UILabel *lbl23 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38 + cellHeight *2, 230, cellHeight)];
    lbl23.text = [NSString stringWithFormat:@"%@米",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"distance"]];
    lbl23.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl23];
    
    
    
    
    
    //时间的显示以及操作
    
    int ordertime = [[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"order_time"]intValue];
    NSDate *orderdate = [[NSDate alloc]initWithTimeIntervalSince1970:ordertime];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
    [formatter2 setDateFormat:@"yyy-MM-dd  HH:mm"];
    
    
    UILabel *lbl25 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38 + cellHeight *3, 230, cellHeight)];
    lbl25.text =  [formatter2 stringFromDate:orderdate];
    lbl25.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl25];
    
    
    
    
    
    int ordertime2 = [[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"point_time"]intValue];
    NSDate *orderdate2 = [[NSDate alloc]initWithTimeIntervalSince1970:ordertime2];
    NSDateFormatter *formatter22 = [[NSDateFormatter alloc]init];
    [formatter22 setDateFormat:@"yyy-MM-dd  HH:mm"];
    
    
    UILabel *lbl26 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38 + cellHeight *4, 230, cellHeight)];
    lbl26.text =  [formatter22 stringFromDate:orderdate2];
    lbl26.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl26];
    
    
    
    
    
    UILabel *lbl27 = [[UILabel alloc]initWithFrame:CGRectMake(90, 38 + cellHeight *5, 230, cellHeight)];
    lbl27.text =  [NSString stringWithFormat:@"%@",[[self.list2 objectAtIndex:indexPath.row] objectForKey:@"address"]];
    lbl27.font = [UIFont systemFontOfSize:13];
    [cell addSubview:lbl27];
    
    [lbl27 setNumberOfLines:0];
    CGSize size = CGSizeMake(230, cellHeight);
    CGSize labelMSSize = [lbl27.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    [lbl27 setFrame:CGRectMake(90, 38 + cellHeight *5+5, labelMSSize.width , labelMSSize.height)];

    
    
    
    //设置不同的产品类目
    
    
    NSDictionary *dict = [self.list2 objectAtIndex:indexPath.row];
    
    int a = 0;
    
    for (int i = 0 ; i < [[dict objectForKey:@"product_list"] count]; i++)
    {
        
        UILabel *lbl28 = [[UILabel alloc]initWithFrame:CGRectMake(0, 38+cellHeight *6+25+i *40, ViewWidth, 40)];
        lbl28.backgroundColor = [UIColor whiteColor];
        lbl28.text = [NSString stringWithFormat:@"  %@",[[dict objectForKey:@"product_list"][i] objectForKey:@"product_name"]];
        lbl28.font = [UIFont systemFontOfSize:15];
        [cell addSubview:lbl28];
        
        
        
        UILabel *lbl281 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-190, 38+cellHeight *6+25+i *40, ViewWidth, 40)];
        lbl281.text = [NSString stringWithFormat:@"￥%@     X%@      ￥%@",[[dict objectForKey:@"product_list"][i] objectForKey:@"unit_price"],[[dict objectForKey:@"product_list"][i] objectForKey:@"product_num"],[[dict objectForKey:@"product_list"][i] objectForKey:@"total_price"]];
        lbl281.font = [UIFont systemFontOfSize:14];
        [cell addSubview:lbl281];
        [lbl281 setNumberOfLines:0];
        CGSize size = CGSizeMake(200,40);
        CGSize labelSize = [lbl281.text sizeWithFont:lbl281.font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
        [lbl281 setFrame:CGRectMake(ViewWidth-200,  12+38+cellHeight *6+25+i *40, labelSize.width, labelSize.height)];
        
        
        
//        UILabel *lbl281 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-190, 38+cellHeight *6+25+i *30, ViewWidth, 30)];
//        lbl281.text = [NSString stringWithFormat:@"￥%@",[[dict objectForKey:@"product_list"][i] objectForKey:@"product_unit"]];
//        lbl281.font = [UIFont systemFontOfSize:15];
//        [cell addSubview:lbl281];
        
        
        
        
//        UILabel *lbl282 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-130, 38+cellHeight *6+25+i *30, ViewWidth, 30)];
//        lbl282.text = [NSString stringWithFormat:@"X%@",[[dict objectForKey:@"product_list"][i] objectForKey:@"product_num"]];
//        lbl282.font = [UIFont systemFontOfSize:15];
//        [cell addSubview:lbl282];
//        
//        
//        
//        UILabel *lbl283 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-75, 38+cellHeight *6+25+i *30, ViewWidth, 30)];
//        lbl283.text = [NSString stringWithFormat:@"￥%@",[[dict objectForKey:@"product_list"][i] objectForKey:@"total_price"]];
//        lbl283.font = [UIFont systemFontOfSize:15];
//        [cell addSubview:lbl283];
//        
        
        
        
        
        a++;
    }
    
    
    //    NSLog(@"jjjjjjjjjiiiii  %d",a);
    
    //按照购买产品的类目生成总的价格
    
//    UILabel *lbl29 = [[UILabel alloc]initWithFrame:CGRectMake(0, 38+cellHeight *6+25+a*40, ViewWidth, 54)];
//    lbl29.text = [NSString stringWithFormat:@"  货款合计   ￥%@",[dict objectForKey:@"wholesaler_amount"]];
//    lbl29.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.4];
//    lbl29.font = [UIFont systemFontOfSize:16];
//    [cell addSubview:lbl29];
    
    
    
    
    UILabel *lbl29 = [[UILabel alloc]initWithFrame:CGRectMake(0, 38+cellHeight*6+25+a*40, 100, 54)];
    lbl29.text = @"  货款合计";
    lbl29.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.4];
    lbl29.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl29];
    
    
    
    UILabel *lbl30 = [[UILabel alloc]initWithFrame:CGRectMake(lbl29.frame.size.width, 38+cellHeight*6+25+a*40, ViewWidth -lbl29.frame.size.width, 54)];
    lbl30.text = [NSString stringWithFormat:@"  ￥%@    ",[dict objectForKey:@"wholesaler_amount"]];
    lbl30.textAlignment = NSTextAlignmentRight;
    lbl30.font = [UIFont systemFontOfSize:16];
    [cell addSubview:lbl30];
    lbl30.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.4];
    lbl30.textColor = BenSe_COLOR;

    
    
    
    
    //添加View之间的背景
    UIView *viewBack = [[UIView alloc]initWithFrame:CGRectMake(0, 38+cellHeight *6+25+a*40 +54, ViewWidth, 20)];
    viewBack.backgroundColor = BACKGROUND1;
    [cell addSubview:viewBack];
    
    
    
    
    
    
    
    
    
    
    return cell;
    
    
    
    
    
}







@end
